package com.lemon.core.common.web.http;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * @author lottery
 * @ClassName DownLoadUtils
 * @desc web下载文件工具类
 * @Date 2018/11/12 15:00
 * @Version 1.0
 **/
public class DownLoadUtils {

    /**
     * 读文件流的大小
     */
    private static final int STREAM_SIZE = 1024;

    /**
     * 文件编码格式-UTF-8
     */
    private static final String UTF_FILE_CODE = "UTF-8";
    /**
     * 响应头-名字
     */
    private static final String RESPONSE_HEADER_NAME = "Content-Disposition";
    /**
     * 响应头-值
     */
    private static final String RESPONSE_HEADER_VALUE = "attachment;filename=";
    /**
     * 响应文本类型
     */
    private static final String RESPONSE_CONTENT_TYPE = "application/octet-stream;charset=";

    /**
     * web文件下载
     * @param fileName
     * @param file
     * @param request
     * @param response
     */
    public static void downLoad(String fileName, File file, HttpServletRequest request, HttpServletResponse response) {
        String prefixName = fileName.substring(0, fileName.lastIndexOf("."));
        String sufixName = fileName.substring(fileName.lastIndexOf("."), fileName.length());

        // 设定头和字符集
        response.setHeader(RESPONSE_HEADER_NAME, RESPONSE_HEADER_VALUE + handleBrowserMessyCode(request, prefixName) + sufixName);
        response.setContentType(RESPONSE_CONTENT_TYPE + UTF_FILE_CODE);
        response.setCharacterEncoding(UTF_FILE_CODE);

        BufferedInputStream inputStream = null;
        BufferedOutputStream outputStream = null;
        try {
            outputStream = new BufferedOutputStream(response.getOutputStream());
            inputStream = new BufferedInputStream(new FileInputStream(file));
            // 二进制下载
            int len = 0;
            byte[] buff = new byte[STREAM_SIZE];
            while ((len = inputStream.read(buff)) != -1 ) {
                outputStream.write(buff, 0, len);
            }
            // 刷新
            outputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != inputStream) {
                    inputStream.close();
                }

                if (null != outputStream) {
                    outputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * ie,chrome,firfox下处理文件名显示乱码
     * @author: hdzhang@iflytek.com
     * @createTime: 2016年11月21日 下午3:28:34
     * @history:
     * @param request
     * @param fileName
     * @return String
     */
    private static String handleBrowserMessyCode(HttpServletRequest request, String fileName) {
        String codedFileName = null;
        try {
            String agent = request.getHeader("USER-AGENT");
            // ie
            if (null != agent && -1 != agent.indexOf("MSIE") || null != agent
                    && -1 != agent.indexOf("Trident")) {
                String name = java.net.URLEncoder.encode(fileName, "UTF8");
                codedFileName = name;
                // 火狐,chrome等
            } else if (null != agent && -1 != agent.indexOf("Mozilla")) {
                codedFileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return codedFileName;
    }
}
